package com.nuanshui.heatedloan.msgcenter.apppush.filter;

import com.nuanshui.heatedloan.dal.IdTypeHandler;
import com.nuanshui.heatedloan.msgcenter.apppush.annotation.AppPushFilterProvider;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.AppPushResult;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.Invoker;
import com.nuanshui.heatedloan.msgcenter.facade.MsgReadStatusService;
import com.nuanshui.heatedloan.msgcenter.model.AppPushMessage;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * 用户为未读取的信息条数数据记录
 * @param 
 * @author guohao
 * @date 2017年1月12日 下午4:13:51
 * @since $
 */
@AppPushFilterProvider(value="notReadNumFilter",order=1000)
public class NotReadNumFilter implements AppPushFilter {

	@Autowired
	private MsgReadStatusService msgReadStatusService;

	@Override
	public AppPushResult invoke(Invoker invoker, AppPushMessage appPushMessage) {
		
		for(HashMap<String, Object> map :appPushMessage.getUserTokens()){
			String userId = IdTypeHandler.encode((Long) map.get("userId"));
			String key =  msgReadStatusService.getReadNum(userId);
			map.put("num", key);
		}
		AppPushResult appPushResult =  invoker.doInvoker(appPushMessage);
		if(appPushResult.isSuccess()) {
			for(String userId :appPushMessage.getMsgBody().getDstUserIds()) {
				msgReadStatusService.incrementAndGetReadNum(userId);
			}
		}		
		return appPushResult;
	}

}
